var DragTableConfigModel = function (data) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (data) {
      self.id = SysUtil.getId();
      this.setPosition(data);

      var tableData = data.tableData;
      if (tableData === undefined) {
        tableData = [];
        var rowNum = self.rowNum;
        var colNum = self.colNum;
        for (var i = 0; i < rowNum; i++) {
          var rowData = [];
          for (var j = 0; j < colNum; j++) {
            rowData.push('');
          }
          tableData.push(rowData);
        }
      }
      self.tableData = tableData;
    },

    setPosition: function (data) {
      var x = data.x || 0;
      var y = data.y || 0;
      var width = data.width;
      if (width === undefined) {
        width = 50 * data.colNum;
      }
      var height = data.height;
      if (height === undefined) {
        height = 30 * data.rowNum;
      }
      self.x = x;
      self.y = y;
      self.width = width;
      self.height = height;

      var widthList = data.widthList;
      if (widthList === undefined) {
        self.colNum = data.colNum;
        self.minWidth = width;
        widthList = this.getAvgNum(width, data.colNum);
      } else {
        var colNum = widthList.length;
        self.colNum = colNum;
        self.minWidth = colNum * 50;
      }
      self.widthList = widthList;

      var heightList = data.heightList;
      if (heightList === undefined) {
        self.rowNum = data.rowNum;
        self.minHeight = height;
        heightList = this.getAvgNum(height - 1, data.rowNum, true);
      } else {
        var rowNum = heightList.length;
        self.rowNum = rowNum;
        self.minHeight = rowNum * 30;
      }
      self.heightList = heightList;
    },

    getAvgNum: function (totalNum, count, accurate) {
      var num = totalNum / count;
      num = Math.floor(num);
      var numList = [];
      for (var i = 0; i < count; i++) {
        numList.push(num);
      }

      if (accurate) {
        var remainderNum = totalNum % count;
        for (var i = 0; i < numList.length; i++) {
          if (remainderNum === 0) {
            break;
          }
          numList[i] += 1;
          remainderNum--;
        }
      }

      return numList;
    }
  };

  this.resetHeight = function () {
    var height = this.height;
    var rowNum = this.rowNum;
    var heightList = fn.getAvgNum(height - 1, rowNum, true);
    this.heightList = heightList;
  };

  this.resetWidth = function ($col) {
    var widthList = [];
    for (var i = 0; i < $col.length; i++) {
      var $td = $($col[i]);
      var width = $td.width();
      widthList.push(width + 2);
    }
    self.widthList = widthList;
  };

  this.checkValid = function (elRect) {
    var tableX = this.x + this.width;
    if (tableX > elRect.width) {
      return 'otherTable';
    }
    var tableY = this.y + this.height;
    if (tableY > elRect.height) {
      return 'otherTable';
    }
    return '';
  };

  this.getObject = function () {
    var postData = {
      x: this.x,
      y: this.y,
      width: this.width,
      height: this.height,
      widthList:this.widthList,
      heightList:this.heightList,
      tableData: this.tableData
    };

    return postData;
  };

  fn.initData(data);
};

var DragConfigModel = function (data) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (data) {
      self.height = data.height;
      self.projectImgConfig = data.projectImg;

      var otherTable = data.otherTable;
      var otherTableList = [];
      for (var i = 0; i < otherTable.length; i++) {
        var option = otherTable[i];
        var table = new DragTableConfigModel(option);
        otherTableList.push(table);
      }
      self.otherTableList = otherTableList;
    }
  };

  this.addTable = function (option) {
    var table = new DragTableConfigModel(option);
    this.otherTableList.push(table);
  };

  this.deleteTable = function (id) {
    var otherTableList = this.otherTableList;
    var index = _.findIndex(otherTableList, {
      id: id
    });
    if (index > -1) {
      otherTableList.splice(index, 1);
    }
  };

  this.checkProjectImgValid = function (elRect) {
    var projectImgConfig = this.projectImgConfig;
    var imgX = projectImgConfig.x + projectImgConfig.width;
    if (imgX > elRect.width) {
      return 'projectImg';
    }
    var imgY = projectImgConfig.y + projectImgConfig.height;
    if (imgY > elRect.height) {
      return 'projectImg';
    }
    return '';
  };

  this.checkOtherTableValid = function (elRect) {
    var otherTableList = this.otherTableList;
    for (var i = 0; i < otherTableList.length; i++) {
      var otherTable = otherTableList[i];
      var code = otherTable.checkValid(elRect);
      if (code !== '') {
        return code;
      }
    }
    return '';
  };

  this.getObject = function () {
    var postData = {
      height: this.height,
      projectImg: this.projectImgConfig
    };

    var otherTableList = this.otherTableList;
    otherTableList = _.map(otherTableList, function (otherTable) {
      return otherTable.getObject();
    });
    postData.otherTable = otherTableList;

    return postData;
  };

  fn.initData(data);
};
